package org.dromara.travel.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.model.PasswordLoginBody;
import org.dromara.common.excel.core.ExcelResult;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.travel.domain.vo.FXcxInterestTagsVo;
import org.dromara.travel.domain.bo.FXcxInterestTagsBo;
import org.dromara.travel.service.IFXcxInterestTagsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户兴趣标签
 *
 * @author zhangshuming
 * @date 2024-04-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/travel/xcxInterestTags")
public class FXcxInterestTagsController extends BaseController {

    private final IFXcxInterestTagsService fXcxInterestTagsService;

    /**
     * 查询用户兴趣标签列表
     */
    @SaCheckPermission("travel:xcxInterestTags:list")
    @GetMapping("/list")
    public TableDataInfo<FXcxInterestTagsVo> list(FXcxInterestTagsBo bo, PageQuery pageQuery) {
        return fXcxInterestTagsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户兴趣标签列表
     */
    @SaCheckPermission("travel:xcxInterestTags:export")
    @Log(title = "用户兴趣标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FXcxInterestTagsBo bo, HttpServletResponse response) {
        List<FXcxInterestTagsVo> list = fXcxInterestTagsService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户兴趣标签", FXcxInterestTagsVo.class, response);
    }

    /**
     * 导出模板
     */
    @PostMapping("/export-template")
    public void  exportTemplate(HttpServletResponse response) {
         ExcelUtil.exportExcel(new ArrayList<>(), "用户兴趣标签", FXcxInterestTagsVo.class, response);
     }

    /**
     * 标签导入
     */
    @Log(title = "用户兴趣标签", businessType = BusinessType.IMPORT)
    @SaCheckPermission("travel:xcxInterestTags:import")
    @PostMapping(value = "/import-data",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(MultipartFile file, boolean updateSupport) throws IOException {
        ExcelResult<FXcxInterestTagsVo> result =  ExcelUtil.importExcel(file.getInputStream(), FXcxInterestTagsVo.class,updateSupport);
        fXcxInterestTagsService.insertList(result.getList());
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取用户兴趣标签详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("travel:xcxInterestTags:query")
    @GetMapping("/{id}")
    public R<FXcxInterestTagsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(fXcxInterestTagsService.queryById(id));
    }

    /**
     * 新增用户兴趣标签
     */
    @SaCheckPermission("travel:xcxInterestTags:add")
    @Log(title = "用户兴趣标签", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FXcxInterestTagsBo bo) {
        return toAjax(fXcxInterestTagsService.insertByBo(bo));
    }

    /**
     * 修改用户兴趣标签
     */
    @SaCheckPermission("travel:xcxInterestTags:edit")
    @Log(title = "用户兴趣标签", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FXcxInterestTagsBo bo) {
        return toAjax(fXcxInterestTagsService.updateByBo(bo));
    }

    /**
     * 删除用户兴趣标签
     *
     * @param ids 主键串
     */
    @SaCheckPermission("travel:xcxInterestTags:remove")
    @Log(title = "用户兴趣标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(fXcxInterestTagsService.deleteWithValidByIds(List.of(ids), true));
    }
}
